package com.abbas.smartCity.ui.findJob;

import android.view.MenuItem;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.abbas.smartCity.R;
import com.abbas.smartCity.base.BaseActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.Arrays;
import java.util.List;

public class FindJobActivity extends BaseActivity {

    private ViewPager vp;
    private BottomNavigationView bnv;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_find_job;
    }

    @Override
    protected String setCustomTitle() {
        return "找工作";
    }

    @Override
    protected void initView() {
        vp = (ViewPager) findViewById(R.id.vp);
        bnv = (BottomNavigationView) findViewById(R.id.bnv);
    }

    @Override
    protected void initData() {
        final List<Fragment> pagers = Arrays.asList(
                new FindJobFragment(),
                new HistoryDeliverFragment(),
                new MineFragment()
        );
        vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return pagers.get(position);
            }

            @Override
            public int getCount() {
                return pagers.size();
            }

            @Override
            public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
                //super.destroyItem(container, position, object);
            }
        });
    }

    @Override
    protected void initEvent() {
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        bnv.setSelectedItemId(R.id.find);
                        break;
                    case 1:
                        bnv.setSelectedItemId(R.id.history);
                        break;
                    case 2:
                        bnv.setSelectedItemId(R.id.person);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        bnv.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.find:
                        vp.setCurrentItem(0);
                        return true;
                    case R.id.history:
                        vp.setCurrentItem(1);
                        return true;
                    case R.id.person:
                        vp.setCurrentItem(2);
                        return true;
                }
                return false;
            }
        });
    }
}